home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 557 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Problem with stringcopy
  5. Date: Sun, 07 Jan 96 00:10:07 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <820973407snz@genesis.demon.co.uk>
  9. References: <4clguu$9fs@eagle.novo.dk> <6JAN199609050601@erich.triumf.ca>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <6JAN199609050601@erich.triumf.ca>
  16.            bennett@erich.triumf.ca "P.Bennett" writes:
  17.  
  18. >In article <4clguu$9fs@eagle.novo.dk>, morb@novo.dk (Morten Brun) writes...
  19. >>How do i do a partial stringcopy i.e. copy from a specific position in
  20. >>a string and a certain numbers of bytes. I am looking for a function
  21. >>like target = Stringcopy(source, startpos, length)
  22. >>Can anybody help please.
  23. >
  24. >Try strncpy(dest, source, size)
  25. >
  26. >If you want to copy 5 chars starting at the 10th char, do something like:
  27. >        strcpy(dest, source+10, 5);
  28.  
  29. Was that meant to be strncpy?
  30.  
  31. Anyway strncpy is a bad idea since it always writes exactly 'size'
  32. characters to the destination string. This means that if there are at
  33. least 'size' characters available before the null character in the
  34. source string, the destination string won't be null character terminated
  35. and hence not a string. If there are significantly less than 'size'
  36. characters in the source string it spends time null character filling
  37. all the extra ones in the destination string. My earlier suggestion
  38. using strncat doesn't suffer from these problems.
  39.  
  40. Admittedly Morten didn't make clear that he wanted the target array to
  41. end up with a properly terminated string. If not strncpy may do the
  42. job required.
  43.  
  44. >There are several string functions starting with "strn" that deal with a
  45. >specified number of chars, rather than with the whole string.
  46.  
  47. memcpy may also be relevant but you'd have to bounds check for the end
  48. of the source string explicitly.
  49.  
  50. -- 
  51. -----------------------------------------
  52. Lawrence Kirby | fred@genesis.demon.co.uk
  53. Wilts, England | 70734.126@compuserve.com
  54. -----------------------------------------
  55.